﻿namespace AC.ExtendedRenderer.Toolkit.Charting
{
    using System;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.Windows.Forms;

    public class CPrinter
    {
        private bool bFitToPaper;
        private Bitmap bmpBuffer = null;
        private PrintDocument document = new PrintDocument();
        private int nPageCount;
        private int nPagesPrinted;

        public CPrinter()
        {
            this.document.PrintPage += new PrintPageEventHandler(this.OnPrintPage);
        }

        private void OnPrintPage(object sender, PrintPageEventArgs e)
        {
            if (this.FitToPaper)
            {
                e.Graphics.DrawImage(this.BmpBuffer, e.PageBounds, new Rectangle(0, 0, this.BmpBuffer.Width, this.BmpBuffer.Height), GraphicsUnit.Pixel);
            }
            else
            {
                e.Graphics.DrawImageUnscaled(this.BmpBuffer, e.PageBounds.Left, e.PageBounds.Top);
            }
            e.HasMorePages = false;
        }

        public bool Print()
        {
            if (this.BmpBuffer == null)
            {
                return false;
            }
            this.PageCount = 0;
            this.document.Print();
            return true;
        }

        public bool ShowOptions()
        {
            bool flag = false;
            PrintDialog dialog = new PrintDialog();
            dialog.Document = this.document;
            dialog.UseEXDialog = true;
            switch (dialog.ShowDialog())
            {
                case DialogResult.OK:
                case DialogResult.Yes:
                    flag = true;
                    break;
            }
            dialog.Dispose();
            dialog = null;
            return flag;
        }

        public Bitmap BmpBuffer
        {
            get
            {
                return this.bmpBuffer;
            }
            set
            {
                this.bmpBuffer = value;
            }
        }

        public PrintDocument Document
        {
            get
            {
                return this.document;
            }
            set
            {
            }
        }

        public bool FitToPaper
        {
            get
            {
                return this.bFitToPaper;
            }
            set
            {
                this.bFitToPaper = value;
            }
        }

        public int PageCount
        {
            get
            {
                return this.nPageCount;
            }
            set
            {
                this.nPageCount = value;
            }
        }

        public int PagesPrinted
        {
            get
            {
                return this.nPagesPrinted;
            }
            set
            {
                this.nPagesPrinted = value;
            }
        }
    }
}

